<template>
    <div>
        <el-container>
            <el-header class="homeHeader">
                <div class="title">云E办</div>
                <div class="header-right">
                    <!-- 全屏切换按钮-->
                    <div class="btn-fullscreen" @click="handlerFullScreen">
                        <el-tooltip :content="fullscreen?`取消全屏`:`全屏`" placement="bottom">
                            <i class="el-icon-rank"></i>
                        </el-tooltip>
                    </div>
                    <!-- 头像 -->
                    <div class="user-avator">
                        <img :src="user.userFace">
                    </div>
                    <!-- 下拉菜单 -->
                    <el-dropdown class="userInfo" @command="handleCommand">
                    <span class="el-dropdown-link">
                        {{user.name}}
                        <i class="el-icon-caret-bottom"></i>
                    </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item command="userInfo">个人中心</el-dropdown-item>
                            <el-dropdown-item command="setting">设置</el-dropdown-item>
                            <el-dropdown-item command="logout">注销登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </div>
            </el-header>
            <el-container>
                <el-aside width="200px">
                    <el-menu router unique-opened>
                        <el-submenu :index="index + ''" v-for="(item, index) in routes"
                                    :key="index"
                                    v-if="!item.hidden">
                            <template slot="title">
                                <i :class="item.iconCls" style="color: #1accff; margin-right: 5px"></i>
                                <span>{{item.name}}</span>
                            </template>
                            <el-menu-item :index="children.path" v-for="(children, indexj) in item.children">
                                {{children.name}}
                            </el-menu-item>
                        </el-submenu>
                    </el-menu>
                </el-aside>
                <el-main>
                    <el-breadcrumb separator-class="el-icon-arrow-right"
                                   v-if="this.$router.currentRoute.path != '/home'">
                        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>{{this.$router.currentRoute.name}}</el-breadcrumb-item>
                    </el-breadcrumb>
                    <div class="homeWelcome" v-if="this.$router.currentRoute.path == '/home'">欢迎来到云E办系统!</div>
                    <router-view class="homeRouterView"/>
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script>
    export default {
        name: "Home",
        data() {
            return {
                fullscreen: false,
                user: JSON.parse(window.sessionStorage.getItem('user'))
            }
        },
        computed: {
            routes() {
                return this.$store.state.routes;
            }
        },
        methods: {
            // 全屏事件
            handlerFullScreen() {
                if (this.fullscreen) {
                    if (document.exitFullscreen) {
                        document.exitFullscreen();
                    } else if (document.webkitCancelFullScreen) {  // safari , Chrome
                        document.webkitCancelFullScreen();
                    } else if (document.mozCancelFullScreen) {  // firefox
                        document.mozCancelFullScreen();
                    } else if (document.msExitFullScreen) {  // ie
                        document.msExitFullScreen();
                    }
                } else {
                    let element = document.documentElement;
                    if (element.requestFullscreen) {
                        element.requestFullscreen();
                    } else if (element.webkitRequestFullScreen) {
                        element.webkitRequestFullScreen();
                    } else if (element.mozRequestFullScreen) {
                        element.mozRequestFullScreen();
                    } else if (element.msRequestFullScreen) {
                        element.msRequestFullScreen();
                    }
                }
                this.fullscreen = !this.fullscreen;
            },
            // 指令事件
            handleCommand(command) {
                if (command == 'logout') {
                    this.$confirm('此操作将注销登录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        // 注销登录
                        this.postRequest('/logout');
                        // 清空用户信息
                        window.sessionStorage.removeItem('tokenStr');
                        window.sessionStorage.removeItem('user');
                        // 清空菜单
                        this.$store.commit('initRoutes', []);
                        // 跳转到登录页
                        this.$router.replace('/');
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '已取消注销!'
                        });
                    });
                }
            }
        }
    }
</script>

<style scoped>
    .homeHeader {
        position: relative;
        /*background: #409eff;*/
        background: #253041;
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        height: 70px;
        font-size: 22px;
        color: #ffffff;
    }

    .homeHeader .title {
        float: left;
        line-height: 70px;
    }

    .header-right {
        float: right;
        padding-right: 50px;
        display: flex;
        height: 70px;
        align-items: center;
    }

    .homeHeader .userInfo {
        margin-left: 10px;
        cursor: pointer;
    }

    /*全屏按钮*/
    .btn-fullscreen {
        transform: rotate(45deg);
        margin-right: 5px;
        font-size: 24px;
    }

    /*头像*/
    .user-avator {
        margin-left: 20px;
    }

    /*头像样式*/
    .user-avator img {
        display: block;
        width: 40px;
        height: 40px;
        border-radius: 50%;
    }

    .el-dropdown-link {
        color: #ffffff;
        cursor: pointer;
    }

    .homeWelcome {
        text-align: center;
        font-size: 30px;
        font-family: "Ubuntu Condensed";
        color: #409eff;
    }

    .homeRouterView {
        margin-top: 15px;
    }
</style>
